from typing import TypeVar, Iterable


_T = TypeVar("_T")


def _merge_preferences(all_preferences: Iterable[list[_T]]):
    dict_preferences: list[dict[_T, int]] = [dict(zip(s, range(0, len(s)))) for s in all_preferences]
    if len(dict_preferences) == 0:
        return []

    score = -1
    result = set()

    pre_temp: dict[_T, int] = dict_preferences.pop()  # 若无此变量， Pycharm 无法推断类型
    for item, current_score in pre_temp.items():
        for pre_temp in dict_preferences:
            others_score = pre_temp.get(item, -1)
            if others_score == -1:
                break
            current_score += others_score
        else:
            if score == -1 or current_score < score:
                score = current_score
                result = {item}
            elif current_score == score:
                result.add(item)

    return result


def _main():
    input_a = eval(input("请输入 A 的偏好："))
    input_b = eval(input("请输入 B 的偏好："))
    print(list(_merge_preferences([input_a, input_b])))


if __name__ == "__main__":
    _main()
